package com.woyowodaows.utils;

import android.content.Context;
import android.content.Intent;


import com.woyowodaows.activity.home.LoginActivity;

import java.io.Serializable;

/**
 * @author haiwei
 *         判断是否登录
 *         2017/2/22
 */
public class LoginJudge {

    public static Context context;
    public static Class<?> objectActivity;
    public static Object objectData;
    public static String DATA = "1001";
    public static String TYPE = "1002";
    public static boolean isLogin = false;


    /**
     * 判断是否登录 如果没有登录则跳转到指定的界面
     *
     * @param context
     * @param objects
     */
    public static void isLogin(Context context, Class<?> objects, Object objectsData) {
        LoginJudge.objectActivity = objects;
        LoginJudge.context = context;
        LoginJudge.objectData = objectsData;
        boolean isLogin = (boolean) SPUtils.get(context,SPUtils.IS_LOGIN,false);
        Intent intent;
        if (isLogin && objects != null) {
            intent = new Intent(context, objects);
            if (objectsData != null) {
                if (objectsData instanceof Serializable) {
                    intent.putExtra(DATA, (Serializable) objectsData);
                }else{
                    intent.putExtra(DATA, (String) objectsData);
                }
            }
            context.startActivity(intent);
            LoginJudge.isLogin = true;
        } else if (isLogin && objects == null) {
            LoginJudge.isLogin = true;
        } else {
            intent = new Intent(context, LoginActivity.class);
            context.startActivity(intent);
            LoginJudge.isLogin = false;
        }
    }

    /**
     * 退出登录时清掉缓存数据
     */
    public static void clearData() {
        objectActivity = null;
        objectData = null;
        isLogin = false;
//        SPUtils.remove(MyApplication.getApplicationContext(), SPUtils.USER_ID);
//        SPUtils.remove(MyApplication.getApplicationContext(), SPUtils.IS_LOGIN);
//        SPUtils.remove(MyApplication.getApplicationContext(),SPUtils.REQUEST_HEAD_JWT);
//        CacheUserInfoUtils.setUserInfo(null);
//        EventBus.getDefault().post(new MessageEvent("refreshPlatformList"));
    }

    public static boolean startLogin(Context context){
        boolean isLogin = (boolean) SPUtils.get(context,SPUtils.IS_LOGIN,false);
        if (!isLogin){
            Intent intent = new Intent(context, LoginActivity.class);
            context.startActivity(intent);
            LoginJudge.isLogin = false;
            return true;
        }
        return false;
    }


}
